<?php
// +----------------------------------------------------------------------
// | B5LaravelCMF
// +----------------------------------------------------------------------
// | Author: 李恒 <357145480@qq.com>
// +----------------------------------------------------------------------
namespace App\Services;

use App\Models\MapplyOrder;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

/**
 * 微信预约报名订单
 * Class MapplyOrderService
 * @package App\Services
 */
class MapplyOrderService extends BaseService
{
    public function __construct(bool $loadValidate = true)
    {
        $this->setModel(new MapplyOrder());
    }

    public function getList($all = false)
    {
        return parent::getList($all, [], [['id', 'desc']]); // TODO: Change the autogenerated stub
    }

    public function qrcode($url){
        if(!$url) return '';

        $targetFileName=md5($url).'.png';
        $savePath='uploads'.DIRECTORY_SEPARATOR.'mapply' . DIRECTORY_SEPARATOR.'qrcode'.DIRECTORY_SEPARATOR.substr($targetFileName,0,1);
        $uploads = public_path($savePath);

        \Illuminate\Support\Facades\File::isDirectory($uploads) or \Illuminate\Support\Facades\File::makeDirectory($uploads, 0777, true, true);

        $uploads_full=$uploads.DIRECTORY_SEPARATOR.$targetFileName;
        $filePath = DIRECTORY_SEPARATOR . trim($savePath . DIRECTORY_SEPARATOR . $targetFileName, DIRECTORY_SEPARATOR);//前端显示文件地址
        $filePath = str_replace(DIRECTORY_SEPARATOR, '/', $filePath);

        if(!file_exists($uploads_full)){
            QrCode::encoding('UTF-8')->format('png')->generate($url,$uploads_full);
        }
        return $filePath;
    }
}
